/*
  学习目标：创建store - 推荐写法
*/

import { legacy_createStore as createStore } from 'redux';

// 2. 使用单独的常量, 维护state的初始值
const initState = { count: 0, msg: 'hello React', list: [] };

// 1. 推荐使用单独的函数, 定义reducer函数
function countReducer(state = initState, { type }) {
  // 3. 使用switch语句, 替换if语句
  switch (type) {
    case 'add':
      return { ...state, count: state.count + 1 };
    case 'des':
      return { ...state, count: state.count - 1 };
    default:
      return state;
  }
}

const store = createStore(countReducer);

store.dispatch({ type: 'add' });
store.dispatch({ type: 'add' });

const unFn = store.subscribe(() => {
  console.log(store.getState(), 'getState');
});

store.dispatch({ type: 'add' });
store.dispatch({ type: 'add' });
store.dispatch({ type: 'des' });
unFn();
store.dispatch({ type: 'des' });
